package com.iplant.wms.serviceImpl.dao.mss;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.mss.MSSTransferRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MSSTransferRuleDAO extends BaseDAO {

    private static MSSTransferRuleDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(MSSTransferRuleDAO.class);

    private MSSTransferRuleDAO() {
        super();

        setTableName("mss_transferrule");
    }

    public static MSSTransferRuleDAO getInstance() {
        if (Instance == null) {
            Instance = new MSSTransferRuleDAO();
        }
        return Instance;
    }

    public void MSS_UpdateTransferRule(
            BMSEmployee wLoginUser, MSSTransferRule wTransferRule, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wTransferRule == null || StringUtils.isEmpty(wTransferRule.Code)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            MSSTransferRule wTransferRuleO = this.MSS_CheckTransferRule(wLoginUser, wTransferRule, wErrorCode);
            if (wTransferRuleO.ID > 0) {
                wErrorCode.set(ExceptionTypes.Duplication.getValue());
            }
            if (wErrorCode.Result != 0) {
                return;
            }

            Map<String, Object> wParwms = new HashMap<String, Object>();

            wParwms.put("RuleID", wTransferRule.BatchRuleID);
            wParwms.put("Name", wTransferRule.Name);
            wParwms.put("Code", wTransferRule.Code);
            wParwms.put("Description", wTransferRule.Description);

            wParwms.put("EditorID", wLoginUser.getID());
            wParwms.put("EditTime", Calendar.getInstance());


            if (wTransferRule.ID > 0) {
                wParwms.put("ID", wTransferRule.ID);
                super.Update(super.getTableName("mss_transferrule"), "ID", wParwms);
            } else {
                wParwms.put("CreatorID", wLoginUser.getID());
                wParwms.put("CreateTime", Calendar.getInstance());
                wParwms.put("Active", 0);
                wTransferRule.ID = super.Insert(super.getTableName("mss_transferrule"), wParwms);

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_UpdateTransferRule", ex);
        }
    }

    public void MSS_DeleteTransferRule(
            BMSEmployee wLoginUser, MSSTransferRule wTransferRule, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wTransferRule == null || wTransferRule.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            this.DeletePO(wLoginUser, wTransferRule, wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_UpdateTransferRule", ex);
        }
    }


    public void MSS_ActiveTransferRule(
            BMSEmployee wLoginUser, MSSTransferRule wTransferRule, int wActive, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        // 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

        try {
            if (wTransferRule == null || wTransferRule.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParwms = new HashMap<String, Object>();

            wParwms.put("ID", wTransferRule.ID);
            wParwms.put("EditorID", wLoginUser.getID());
            wParwms.put("EditTime", Calendar.getInstance());
            wParwms.put("Active", wActive);
            super.Update(super.getTableName("mss_transferrule"), "ID", wParwms);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_ActiveTransferRule", ex);
        }
    }

    public List<MSSTransferRule> MSS_QueryTransferRuleList(
            BMSEmployee wLoginUser, String wName, int wBatchRuleID,String wBatchRuleCode,int wActive, Pagination wPagination,
            OutResult<Integer> wErrorCode) {
        return this.MSS_QueryTransferRuleList(wLoginUser, -1, "", wName, wBatchRuleID,wBatchRuleCode,

                wActive, wPagination, wErrorCode);
    }

    private List<MSSTransferRule> MSS_QueryTransferRuleList(
            BMSEmployee wLoginUser, int wID, String wCode, String wName, int wBatchRuleID,String wBatchRuleCode,

            int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<MSSTransferRule> wMSSTransferRuleList = new ArrayList<MSSTransferRule>();
        wErrorCode.set(0);

        try {

            // Step02:查询
            Map<String, Object> wParwms = new HashMap<String, Object>();
            String wSQLText = "";
            wSQLText = StringUtils.Format("Select t.*,t1.Name as ShelvesRuleName,t1.Code as ShelvesRuleCode," +
                                          " t3.Name as CreatorName ,t4.Name as EditorName  from {0}.mss_transferrule t  " +
                                          " left join {0}.mss_batchrule t1 on t.BatchRuleID=t1.ID " +
                                          " left join {0}.mbs_user t3 on t.CreatorID=t3.ID " +
                                          " left join {0}.mbs_user t4 on t.EditorID=t4.ID " +
                                          " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code) " +
                                          " AND (:BatchRuleID<=0 or  t.BatchRuleID=:BatchRuleID)  " +
                                          " AND (:BatchRuleCode ='''' or  t1.Code=:BatchRuleCode)  " +
                                          " AND (:Active<0 or  t.Active=:Active) AND (:Name = '''' or  t.Name like :Name) ",
                    super.getDBName());

            wSQLText = this.DMLChange(wSQLText);

            wParwms.put("ID", wID);
            wParwms.put("Code", wCode);
            wParwms.put("BatchRuleID", wBatchRuleID);
            wParwms.put("BatchRuleCode", wBatchRuleCode);
            wParwms.put("Active", wActive);
            wParwms.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

            wMSSTransferRuleList = super.QueryForList(wSQLText, wParwms, wPagination, MSSTransferRule.class);

            // Step03:配置系统
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryTransferRuleList", ex);
        }
        return wMSSTransferRuleList;
    }

    public MSSTransferRule MSS_QueryTransferRule(
            BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
        MSSTransferRule wResult = new MSSTransferRule();
        wErrorCode.set(0);

        try {
            List<MSSTransferRule> wList = null;
            if (wID > 0) {
                wList = this.MSS_QueryTransferRuleList(
                        wLoginUser, wID, "", "", -1, "",-1, Pagination.getDefault(), wErrorCode);

            } else if (StringUtils.isNotEmpty(wCode)) {
                wList = this.MSS_QueryTransferRuleList(
                        wLoginUser, -1, wCode, "", -1, "",-1, Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryTransferRuleByID", ex);
        }
        return wResult;
    }


    public MSSTransferRule MSS_CheckTransferRule(
            BMSEmployee wLoginUser, MSSTransferRule wMSSTransferRule, OutResult<Integer> wErrorCode) {
        MSSTransferRule wTransferRule = new MSSTransferRule();
        wErrorCode.set(0);

        try {

            // Step0:查询
            Map<String, Object> wParwms = new HashMap<String, Object>();
            String wSQLText = StringUtils.Format("Select * from {0}.mss_transferrule " + " where ID!=:ID  " +
                                                 "  AND  (BatchRuleID=:BatchRuleID or Name =:Name or (:Code != '''' and Code=:Code)) ",
                    super.getDBName());
            wParwms.clear();
            wParwms.put("ID", wMSSTransferRule.ID);
            wParwms.put("BatchRuleID", wMSSTransferRule.BatchRuleID);
            wParwms.put("Name", wMSSTransferRule.Name);
            wParwms.put("Code", wMSSTransferRule.Code);

            wSQLText = this.DMLChange(wSQLText);
            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wTransferRule.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wTransferRule.BatchRuleID = StringUtils.parseInt(wSqlDataReader.get("BatchRuleID"));
                wTransferRule.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wTransferRule.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

                wTransferRule.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wTransferRule.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wTransferRule.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wTransferRule.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wTransferRule.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_CheckTransferRule", ex);
        }
        return wTransferRule;
    }

}
